package com.ruoyi.bussiness.utils;

import com.ruoyi.bussiness.domain.VideoInfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author 12290
 */
public class ffmpegTools {

    public static VideoInfo getVideoInfo(String filePath){
        VideoInfo videoInfo = new VideoInfo();
        try {
            ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-i", filePath);
            Process process = processBuilder.start();

            // Read the output from ffmpeg
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("Duration")) {
                    // Extract the duration from the output
                    int index = line.indexOf("Duration: ");
                    if (index != -1) {
                        videoInfo.setDuration(line.substring(index + 10, index + 21));
                        break;
                    }
                }
            }

            // Wait for the process to finish
            process.waitFor();

            // Close the reader
            reader.close();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

        return videoInfo;
    }
}
